package com.company.tree;

public class BalancedTree {


    private DeepOfTree deepOfTree=new DeepOfTree();

    public boolean isBalancedTree(TreeNode root){


        return check(root);

    }
    private boolean check(TreeNode r){

        if (r==null) {
            return true;
        }

        int leftDeep = this.deepOfTree.getDeepOfTree(r.left);
        int rightDeep = this.deepOfTree.getDeepOfTree(r.right);


        if (Math.abs(leftDeep-rightDeep)<2) {
            return check(r.left)&&check(r.right);
        }else {
            return false;
        }
    }

}
